#include <iostream>
#include <map>
#include <string>
#include <utility>

using std::cout;
using std::endl;
using std::map;
using std::string;
using std::pair;
using std::make_pair;

template <typename Container>
void display(const Container &con)
{
    for(auto &elem : con)
    {
        cout << elem.first << "  " << elem.second << endl;
    }
}

void test()
{
    //map的特征
    //1、存放的是key-value类型，key值是唯一的，不能重复，
    //但是value是既可以重复也可以不重复
    //2、默认会按照key值进行升序排列
    //3、底层使用的是红黑树
    map<int, string> number = {
        {1, "hello"},
        {2, "beijing"},
        pair<int, string>(6, "wuhan"),
        pair<int, string>(7, "nanjing"),
        make_pair(1, "wangdao"),
        make_pair(3, "beijing"),
    };
    display(number);

    cout << endl << "map的insert操作" << endl;
    pair<map<int, string>::iterator, bool> ret 
    /* number.insert(pair<int, string>(5, "shenzhen")); */
    /* number.insert(make_pair(5, "shenzhen")); */
        = number.insert({5, "shenzhen"});
    if(ret.second)
    {
        cout << "插入成功 " << ret.first->first << "   "
             << ret.first->second << endl;
    }
    else
    {
        cout << "插入失败，该元素存在map中" << endl;
    }
    display(number);

    cout << endl << "map的删除" << endl;
    auto it = number.begin();
    number.erase(it);
    display(number);
}

int main(int argc, char *argv[])
{
    test();
    return 0;
}

